package com.mk.common;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 11:40 AM 3/16/18
 */
public class FileUtils {

    /**
     * MultipartFile 转换成File
     *
     * @param multfile 原文件类型
     * @return File
     * @throws IOException
     */
    public static File multipartToFile(MultipartFile multfile) throws IOException {
        CommonsMultipartFile cf = (CommonsMultipartFile)multfile;
        //这个myfile是MultipartFile的
        DiskFileItem fi = (DiskFileItem) cf.getFileItem();
        File file = fi.getStoreLocation();
        //手动创建临时文件
        if(file.length() < FileConfig.MIN_FILE_SIZE){
            File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +
                    file.getName());
            multfile.transferTo(tmpFile);
            return tmpFile;
        }
        return file;
    }

    public static MultipartFile fileToMultipartFile(File file) throws IOException {
        MultipartFile multipartFile = new MockMultipartFile(file.getName(),file.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(),
                new FileInputStream(file));
        return multipartFile;
    }

    /**
     * 获取文件扩展名
     * @param filename
     * @return
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /**
     * 获取不带扩展名的文件名
     * @param filename
     * @return
     */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }

    public static Boolean isOffice(String name){
        String offices = ".doc.docx.xls.xlsx.ppt.pptx.csv";
        return offices.contains(getExtensionName(name));
    }

    public static Boolean isOther(String name){
        String others = ".pdf.png.PNG.JPG.jpg.gif.bmp.jpeg.JPEG.mp3.wav.ogg.mp4.webm.txt";
        return others.contains(getExtensionName(name));
    }

    /**
     * 删除单个文件
     * @param   fileName    被删除文件的文件名
     * @return 单个文件删除成功返回true,否则返回false
     */
    public static boolean deleteFile(String fileName){
        File file = new File(fileName);
        if (file.isFile() && file.exists()) {
            file.delete();//"删除单个文件"+name+"成功！"
            return true;
        }//"删除单个文件"+name+"失败！"
        return false;
    }


    /**
     * 删除某个文件夹下的所有文件夹和文件,不包括其本身
     * @param delpath
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static boolean deleteDirAndFiles(String delpath){
        File dir = new File(delpath);
        if (dir.exists() && dir.isDirectory()){
            File[] files = dir.listFiles();
            for (int i=0;i<files.length;i++){
                File f = files[i];
                if (f.isDirectory()){
                    deleteDirAndFiles(f.getPath());
                }else {
                    f.delete();
                }
            }
        }
        return true;
    }

    public static boolean deleteFolder(String url) {
        File file = new File(url);
        if (!file.exists()) {
            return false;
        }
        if (file.isFile()) {
            file.delete();
            return true;
        } else {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                String root = files[i].getAbsolutePath();//得到子文件或文件夹的绝对路径
                //System.out.println(root);
                deleteFolder(root);
            }
            file.delete();
            return true;
        }
    }
}
